import {
  defineStore
} from "pinia";
import {
  loginAPI
} from "@/apis/user";

import {
  ref
} from "vue";
import { useCartStore } from "./cart";
export const useUserStore = defineStore('user', () => {
  const userInfo = ref({})
  const cartStore = useCartStore()
  //得到用户信息
  const getSerInfo = async (account, password) => {
    const res = await loginAPI(account, password);
    if (res.code === "1") {
      userInfo.value = res.result
      cartStore.mergeCart()
      cartStore.getNewCart()
    }
  }
  //清除用户信息
  const clearUserInfo = () => {
    userInfo.value = {}
    cartStore.clearCart()
  }
  return {
    userInfo,
    getSerInfo,
    clearUserInfo
  }
}, {
  persist: true,
}, )
